package org.erepublik.company.controllers;

import javax.servlet.http.HttpServletRequest;

import org.erepublik.company.CompanyCalculator;
import org.erepublik.company.CountryData;
import org.erepublik.company.ECountry;
import org.erepublik.company.searchCriteria.CompanySimulatorFetch;
import org.erepublik.company.searchCriteria.ECompanyCalculatorProductType;
import org.erepublik.company.searchCriteria.EQuality;
import org.erepublik.company.searchCriteria.ERawRegion;
import org.erepublik.company.searchCriteria.EWorkerWellness;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.service.JSONCountryDataProvider;
import org.erepublik.company.service.XmlVatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/companySimulator.html")
public class CompanySimulatorController extends AbstractController {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @Autowired
    private JSONCountryDataProvider dataService;  
    
    @Autowired
    private XmlVatService vatService;  
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap model) {
        fillModelMap(model);
      
        CompanyCalculator company = null;
        String attribute = request.getParameter("country");
        if (attribute!=null) {
            CompanySimulatorFetch criteria = new CompanySimulatorFetch();
            criteria.setCountry( ECountry.valueOf(request.getParameter("country")) );
            criteria.setProductType( ECompanyCalculatorProductType.valueOf( request.getParameter("productType") ) );
            criteria.setQuality( EQuality.valueOf( "Q"+request.getParameter("quality"))  );
            criteria.setRawRegion( ERawRegion.valueOf( request.getParameter("rawRegion"))  );
            criteria.setWorkerWellness(EWorkerWellness.W90);
            
            company = new CompanyCalculator(productService, vatService,  criteria);
            company.calculateProfit();
            CountryData country = dataService.getCountryData(company.getCompany().getCountry());

            double currency = country.getGoldForCurrency();
            model.addAttribute("currency", currency);
        }
        
        model.addAttribute("company", company);
       
        model.addAttribute("companySimulatorFetch", new CompanySimulatorFetch());

        return "companySimulator";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("companySimulatorFetch") CompanySimulatorFetch criteria) {
        fillModelMap(model);

        CompanyCalculator company = new CompanyCalculator(productService, vatService,  criteria);
        company.calculateProfit();
        CountryData country = dataService.getCountryData(company.getCompany().getCountry());
        
        
        double currency = country.getGoldForCurrency();
        model.addAttribute("company", company);
        model.addAttribute("currency", currency);
        model.addAttribute("companyFetch", criteria);
        model.addAttribute("hideBottom", true);
        
        return "companySimulator";
    }
    
}
